package com.example.sls.dao.field;

import com.example.sls.pojo.field.FieldManagement;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 场地信息管理表(FieldManagement)表数据库访问层
 *
 * @author makejava
 * @since 2024-06-18 10:55:12
 */
public interface FieldManagementDao {
  /**
   * 通过ID查询单条数据
   *
   * @param id 主键
   * @return 实例对象
   */
  @Select("select * from field_management where id=#{id}")
  FieldManagement queryById(Integer id);

  /**
   * 查询指定行数据
   *
   * @param fieldManagement 查询条件
   * @param pageSize        分页对象
   * @param offset          偏移量，起始处
   * @return 对象列表
   */
  @Select("select * from field_management where 1=1" +
          " and (id=#{fieldManagement.id} or (isnull(#{fieldManagement.id}) or #{fieldManagement.id}=0))" +
          " and (field_id=#{fieldManagement.fieldId} or (isnull(#{fieldManagement.fieldId}) or #{fieldManagement.fieldId}=0))" +
          " and (name=#{fieldManagement.name} or (isnull(#{fieldManagement.name}) or #{fieldManagement.name}=''))" +
          " and (location=#{fieldManagement.location} or (isnull(#{fieldManagement.location}) or #{fieldManagement.location}=''))" +
          " and (status=#{fieldManagement.status} or (isnull(#{fieldManagement.status}) or #{fieldManagement.status}=''))" +
          " and (created_at=#{fieldManagement.createdAt} or (isnull(#{fieldManagement.createdAt}) or #{fieldManagement.createdAt}=''))" +
          " limit #{pageSize} offset #{offset}")
  List<FieldManagement> queryAllByLimit(FieldManagement fieldManagement, Integer pageSize, Integer offset);

  /**
   * 统计总行数
   *
   * @return 总行数
   */
  @Select("select count(*) from field_management")
  long count();

  /**
   * 新增数据
   *
   * @param fieldManagement 实例对象
   * @return 影响行数
   */
  @Insert("insert into field_management (id,field_id,name,location,status,created_at) " +
          "values(" +
          "if(#{id}!=0 and !isnull(#{id}),#{id},null)," +
          "if(#{fieldId}!=0 and !isnull(#{fieldId}),#{fieldId},null)," +
          "if(#{name}!='' and !isnull(#{name}),#{name},null)," +
          "if(#{location}!='' and !isnull(#{location}),#{location},null)," +
          "if(#{status}!='' and !isnull(#{status}),#{status},null)," +
          "if(#{createdAt}!='' and !isnull(#{createdAt}),#{createdAt},null)" +
          ")")
  int insert(FieldManagement fieldManagement);

  /**
   * 通过id修改数据
   * id 主键
   *
   * @param fieldManagement 实例对象
   * @return 影响行数
   */
  @Update("update field_management set " +
          "field_id=if(isnull(#{fieldId}) or #{fieldId}=0,field_id,#{fieldId})," +
          "name=if(isnull(#{name}) or #{name}='',name,#{name})," +
          "location=if(isnull(#{location}) or #{location}='',location,#{location})," +
          "status=if(isnull(#{status}) or #{status}='',status,#{status})," +
          "created_at=if(isnull(#{createdAt}) or #{createdAt}='',created_at,#{createdAt})" +
          " where id=#{id}")
  int update(FieldManagement fieldManagement);

  /**
   * 通过主键删除数据
   *
   * @param id 主键
   * @return 影响行数
   */
  @Delete("delete from field_management where id=#{id}")
  int deleteById(Integer id);
}

